{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# CMSC 110 Introduction to Computing\n", "\n", "## Spring 2015\n", "\n", "### Professor Blank\n", "\n", "### Assignment #7: SimWorld\n", "\n", "Due before start of class on Monday, April 6, 2015.\n", "\n", "#### NO CREDIT FOR LATE SUBMISSIONS\n", "\n", "**Task**: Create a Simulated Creature that roams around a Simulated World:\n", "\n", "* an animated scene that exhibits movement and captures one's attention for an extended period\n", "\n", "**Specifics**: \n", "\n", "* You should use a canvas that is 640 by 480 pixels. \n", "* Your creature should have internal state\n", "* Your creature will move around, changing position and state\n", "* Objects should draw themselves in the correct position\n", "* Work individually on this project\n", "\n", "**Grading**:\n", "\n", "1. Originality and creativity, 50%\n", "2. Proper use of Objects, 50%\n", "\n", "Hand in:\n", "\n", "* Submit your homework via the \"Submit\" button. \n", "\n", "## Details\n", "\n", "For this assignment, your mission is to create a SimCreature that will live in a sim world.\n", "\n", "For an example of this, I will use `Dog`.\n", "\n", "Dog should have at least four methods:\n", "\n", "1. a constructor with no arguments, that puts them in a random position.\n", "1. a constructor with three arguments, that puts them in (x, y, z).\n", "2. an update method to figure out where they should move next, and to update their internal states.\n", "3. a draw method that draws them in their current position, in current state.\n", "\n", "The basic prototype would therefore look like:\n", "\n", "```java\n", "class Dog {\n", " ...\n", " Dog() {\n", " ...\n", " }\n", " Dog(float x, float y, float z) {\n", " ...\n", " }\n", " void update() {\n", " ...\n", " }\n", " void draw() {\n", " ...\n", " }\n", "}\n", "```\n", "\n", "Where there are ellipses, you will fill out the code.\n", "\n", "To make a bunch of these objects, we need to do three things:\n", "\n", "1. create a global array or list to put them in\n", "2. create the array\n", "3. create each of the objects\n", "\n", "That might look like this:\n", "\n", "```java\n", "Dog [] dogs;\n", "\n", "void setup() {\n", " dogs = new Dog[5];\n", " for (int i = 0; i < dogs.length; i++) {\n", " dogs[i] = new Dog();\n", " }\n", "}\n", "```\n", "\n", "In this example, I created a global array named `dogs`, then created an array of length 5, and then created 5 instances of the `Dog` class. Note that the global array is created outside any function, but the creation of the array, and instances occurs in the `setup()` function.\n", "\n", "Finally, we need to update and redraw the dogs. We do that \n", "\n", "```java\n", "void draw() {\n", " for (int i = 0; i < dogs.length; i++) {\n", " dogs[i].update();\n", " dogs[i].draw();\n", " }\n", "}\n", "```\n", "\n", "## SimWorld\n", "\n", "Now, a bit of a wrinkle. The location of these objects will roam around a new coordinate system that has x from -1 to 1, y from -1 to 1, and z from -1 to 1. (-1,-1,-1) will be in the front, bottom, left-hand corner, and (1,1,1) will be in the back, top, right-hand corner. Furthermore, as you go back further on the z axis, the object will get smaller.\n", "\n", "So, the further away (higher z value) the smaller the object will appear.\n", "\n", "We add two more methods to make this easy on us:\n", "\n", "1. coordinates(x, y, z) - returns the screen coordinates [x,y] of the z, y, z position\n", "1. scale(size, z) - returns how big this size would look at the objects depth" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n", "\n", "\n", "### Code\n", "\n", "In the following simulations, click and drag to change the view. Dragging up (closer) and down (further away) changes the viewer distance; dragging left (smaller) and right (larger) changes the field of view/vision." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "